package com.strive.user.controller;

import com.strive.common.model.base.PageResult;
import com.strive.common.model.base.Result;
import com.strive.common.model.dto.SysPower;
import com.strive.user.model.SysPowerRolesVo;
import com.strive.user.service.ISysPowerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: bingcun.chen
 * @Date: 2022/6/14 11:07
 * @Version 1.0
 */
@RestController
@Api(tags = "权限模块api")
@RequestMapping("/powers")
@Slf4j
public class SysPowerController {

    @Autowired
    private ISysPowerService sysPowerService;

    @PostMapping("/insertPowers")
    public Result<Boolean> insertPowers(@RequestBody List<SysPower> sysPower){
        return Result.succeed(sysPowerService.insertPowers(sysPower));
    }

    /**
     * 设置权限
     * @param sysPowerRolesVo
     * @return
     */
    @ApiOperation(value = "设置权限")
    @PostMapping("/setPowersByRoleId")
    public Result<Boolean> setPowersByRoleId(@RequestBody SysPowerRolesVo sysPowerRolesVo){
        return Result.succeed(sysPowerService.setPowersByRoleId(sysPowerRolesVo));
    }

    /**
     * 权限查询
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "权限查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageNum", value = "分页结束位置", required = true, dataType = "Integer")
    })
    @GetMapping("/getPowerDataByMenuId")
    public PageResult<SysPower> getPowerDataByMenuId(@RequestParam Map<String, Object> params) {
        return sysPowerService.getPowerDataByMenuId(params);
    }


    /**
     * 权限新增或者更新
     *
     * @param sysPower
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SysPower sysPower) throws Exception {
        sysPowerService.saveOrUpdateRole(sysPower);
        return Result.succeed("操作成功");
    }

    /**
     * 后台管理删除角色
     * delete /role/1
     *
     * @param id
     */
    @ApiOperation(value = "后台管理删除权限")
    @DeleteMapping("/{id}")
    public Result deletePower(@PathVariable Long id) {
        try {
            sysPowerService.deletePower(id);
            return Result.succeed("操作成功");
        } catch (Exception e) {
            log.error("role-deleteRole-error", e);
            return Result.failed("操作失败");
        }
    }


}
